// src/utils/timeUtils.ts

export function formatTimeAgo(date: Date | string): string {
  const now = new Date()
  const time = new Date(date)
  const diffInSeconds = Math.floor((now.getTime() - time.getTime()) / 1000)

  const units: { unit: string; seconds: number }[] = [
    { unit: '年', seconds: 60 * 60 * 24 * 365 },
    { unit: '月', seconds: 60 * 60 * 24 * 30 },
    { unit: '天', seconds: 60 * 60 * 24 },
    { unit: '小时', seconds: 60 * 60 },
    { unit: '分钟', seconds: 60 }
  ]

  for (const { unit, seconds } of units) {
    const result = Math.floor(diffInSeconds / seconds)
    if (result >= 1) {
      return `${result}${unit}前`
    }
  }

  return '刚刚'
}
